//
//  CustomDetailViewController.m
//  autostainer
//
//  Created by lurenjia on 2017/6/15.
//  Copyright © 2017年 walker. All rights reserved.
//

#import "CustomDetailViewController.h"
#import "Global.h"

@interface CustomDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *MainView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *doneBtn;

@end

@implementation CustomDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)timeBtnClicked:(CustomBtn *)sender {
    __weak typeof(self) ws = self;
    DataView *dataView = [[DataView alloc]initWithChooseStyle:ChooseStyle_Time CompleteBlock:^(PickResultModel *resultModel) {
        __strong typeof(ws) strongSelf = ws;
        ExperimentModel *model = strongSelf.dataArray[sender.index];
        resultModel.temperature = model.timeModel.temperature;
        model.timeModel = resultModel;
        [strongSelf.dataArray replaceObjectAtIndex:sender.index withObject:model];
        [strongSelf.MainView reloadData];
    }];
    [dataView show];
}
- (void)temperatureBtnClicked:(CustomBtn *)sender {
    __weak typeof(self) ws = self;
    DataView *dataView = [[DataView alloc]initWithChooseStyle:ChooseStyle_Temperature CompleteBlock:^(PickResultModel *resultModel) {
        __strong typeof(ws) strongSelf = ws;
        ExperimentModel *model = strongSelf.dataArray[sender.index];
        NSString *str = resultModel.temperature;
        model.note = str;
        NSLog(@"选中的温度%@",str);
        [strongSelf.dataArray replaceObjectAtIndex:sender.index withObject:model];
        [strongSelf.MainView reloadData];
    }];
    [dataView show];
}
- (IBAction)doneBtnClicked:(UIBarButtonItem *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    CustomReactionDetailViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CustomReactionDetailViewController"];
    vc.navTitleStr = _navTitleStr;
    vc.dataArray = self.dataArray;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark - TableViewDelegate && TableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
    UILabel *label = [cell.contentView viewWithTag:1];
    ExperimentModel *model = self.dataArray[indexPath.section];
    label.text = model.name;
    CustomBtn *timeBtn = [cell.contentView viewWithTag:2];
    timeBtn.index = indexPath.section;
    [timeBtn addTarget:self action:@selector(timeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    CustomBtn *noteBtn = [cell.contentView viewWithTag:3];
    noteBtn.index = indexPath.section;
    [noteBtn addTarget:self action:@selector(temperatureBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    PickResultModel *timeModel = model.timeModel;
    if (timeModel) {
        [timeBtn setTitle:[NSString stringWithFormat:@"%.0f小时%.0f分%.0f秒",timeModel.hour,timeModel.minute,timeModel.second] forState:UIControlStateNormal];
    }else {
        [timeBtn setTitle:@"点击设置" forState:UIControlStateNormal];
    }
    NSString *noteStr = model.note;
    if (noteStr && ![noteStr isEqualToString:@""]) {
        [noteBtn setTitle:[NSString stringWithFormat:@"%@°C",noteStr] forState:UIControlStateNormal];
    }else {
        [noteBtn setTitle:@"点击设置" forState:UIControlStateNormal];
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 15)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 15)];
    label.text = [NSString stringWithFormat:@"第%ld步反应",section+1];
    label.font= [UIFont systemFontOfSize:15];
    label.textColor = [UIColor blackColor];
    [headerView addSubview:label];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 5;
}
@end
